/**
 * Project Name: renshuaipeng-auth
 * File Name: LoginInterceptor.java
 * Package Name: ren.renshuaipeng.auth.interceptor
 * Date: 2015年12月21日 下午1:12:58
 * Copyright (c) 2015, niao.shuai123@163.com All Rights Reserved.
 */
package ren.renshuaipeng.auth.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import ren.renshuaipeng.auth.common.AjaxUtil;
import ren.renshuaipeng.auth.common.AppContants;
import ren.renshuaipeng.auth.common.JSONUtils;
import ren.renshuaipeng.auth.domain.auth.AuthUser;
import ren.renshuaipeng.util.ajax.AjaxDto;

/**
 * ClassName: LoginInterceptor <br/>  
 * Function: TODO. <br/>
 * Date:     2015年12月21日 下午1:12:58 <br/>  
 * @author 任帅鹏（作者）
 * @version V1.00 （版本号）  
 * @since JDK 1.8  
 * @see
 */
public class LoginInterceptor implements HandlerInterceptor{

	//private static final Logger logger=LoggerFactory.getLogger(LoginInterceptor.class);
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//判断是否登录
        String userId=getUserIds(request.getSession());
        //logger.info("userId:"+userId+"session:");
        //返回登录页面
        if(StringUtils.isBlank(userId)){
        	 if(AjaxUtil.checkIsAjaxReq(request)){
                 AjaxUtil.ajaxJsonResponse(response, JSONUtils.obj2json(new AjaxDto(AjaxDto.VALIDATE_CODE,"您未登录!")));
             }else {
                 response.sendRedirect("/login");
             }
            return false;
        }
        return true;
	}

	/**
	 *
	 * @Name: getUserIds
	 * @Description: 获取登录Id
	 * @Author 任帅鹏（作者）
	 * @Version V1.00 （版本号）
	 * @Create Date: 2015年12月21日 下午1:15:34（创建日期）
	 * @Parameters: @param session
	 * @Parameters: @return
	 * @Return String
	 * @Throws
	 */
	private String getUserIds(HttpSession session) {
		AuthUser user=(AuthUser) session.getAttribute(AppContants.LOGIN_SESSION);
        if(user==null){
            return "";
        }
        return user.getId();
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
